package com.dk.wiki.controller;

import com.dk.wiki.req.DocQueryReq;
import com.dk.wiki.req.DocSaveReq;
import com.dk.wiki.resp.CommonResp;
import com.dk.wiki.resp.DocQueryResp;
import com.dk.wiki.resp.PageResp;
import com.dk.wiki.service.DocService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 开发hello接口
 * @author: ldk
 * 描述: TODO
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    @Resource
    private DocService docService;

    // @GetMapping("/list")
    // // @RequestMapping(value = "/helloWord", method = RequestMethod.GET)
    // public CommonResp list(String name) {
    //     CommonResp<List<Doc>> resp = new CommonResp<>();
    //     List<Doc> list = docService.list(name);
    //     resp.setContent(list);
    //     return resp;
    // }

    @GetMapping("/all/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId) {
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> list = docService.all(ebookId);
        resp.setContent(list);
        return resp;
    }

    @GetMapping("/list")
    // @RequestMapping(value = "/helloWord", method = RequestMethod.GET)
    public CommonResp list(@Valid DocQueryReq docQueryReq) {
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        PageResp<DocQueryResp> list = docService.list(docQueryReq);
        resp.setContent(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody DocSaveReq docSaveReq) {
        CommonResp resp = new CommonResp<>();
        docService.save(docSaveReq);
        return resp;
    }

    @DeleteMapping("/delete/{idsStr}")
    public CommonResp delete(@PathVariable String idsStr) {
        CommonResp resp = new CommonResp<>();
        List<String> ids = Arrays.asList(idsStr.split(","));
        docService.delete(ids);
        return resp;
    }

    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id) {
        CommonResp resp = new CommonResp<String>();
        String content = docService.findContent(id);
        resp.setContent(content);
        return resp;
    }

    @GetMapping("/vote/{id}")
    public CommonResp vote(@PathVariable Long id){
        CommonResp commonResp = new CommonResp();
        docService.vote(id);
        return commonResp;
    }
}
